package lanou.giftsay_01.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import lanou.giftsay_01.bean.RollBean;
import lanou.giftsay_01.R;

/**
 * Created by dllo on 16/8/16.
 */
public class VpAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
 private ArrayList<RollBean> arrayList ;
 private   ViewPager viewPager ;
 private Context context ;

    public VpAdapter(Context context) {
        this.context = context;
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
    }

    public void setArrayList(ArrayList<RollBean> arrayList) {
        this.arrayList = arrayList;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return arrayList !=  null ? Integer.MAX_VALUE : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_head , null);
        ImageView imageView = (ImageView)view.findViewById(R.id.iv_lunbo);
        Picasso.with(context).load(arrayList.get(0).getData().getBanners().get(position % arrayList.get(0).getData().getBanners().size()).getImage_url()).into(imageView);
        container.addView(view);
        viewPager.addOnPageChangeListener(this);
        return view ;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
